<?php
namespace Common\Model;

use Think\Model;

class MenuModel extends Model{
    //无表前缀的菜单表名
    protected $table;
    //有表前缀的菜单表
    protected $menuTable;
    //表前缀
    protected $prefix;

    /**构造函数初始化数据
     * @param string $menuTable
     * @param string $prefix
     */
    public function __construct($menuTable,$prefix = ''){
        parent::__construct();
        $this -> prefix = empty($prefix) ? C('DB_PRIFIX') : $prefix;
        $this -> table  = $menuTable;
        $this -> menuTable = $prefix . $menuTable;
    }

    /**添加菜单
     * @param $data
     */

    public function addMenu($data){
        $checkInfo = $this -> checkInfo($data);
        if($checkInfo !== true){
            return $checkInfo;
        }
        $menuData = $this -> formatData($data);
        $menu = M($this -> table);
        $isSAVE = $menu -> add($menuData,[],true);
        if($isSAVE){
            return $this -> returnInfo('操作成功！','');
        }else{
            return $this -> returnInfo('操作失败！','',0);
        }
    }

    /**格式化数据
     * @param $data
     * @return array
     */
    public function formatData($data){
        $id = empty($data['id']) ? 0 : $data['id'];
        $pid = empty($data['pid']) ? 0 : $data['pid'];
        $sort = empty($data['sort']) ? 1 : $data['sort'];
        $type = empty($data['type']) ? 1 : $data['type'];
        $status = empty($data['status']) ? 0 : $data['status'];
        $arr = ['id' => $id, 'pid' => $pid,'title' => $data['title'], 'link' => $data['link'] , 'ico' => $data['ico'] , 'type' => $type, 'sort' => $sort, 'status' => $status];
        return $arr;
    }

    /**检查数据
     * @param $data
     * @return array|bool
     */
    public function checkInfo($data){
        if(empty($data['title'])){
            return $this -> returnInfo('菜单名称不能为空！','',0);
        }
        if(empty($data['link'])){
            return $this -> returnInfo('连接不能为空！','',0);
        }
        if(empty($data['sort']) || (strlen($data['sort']) > 5)){
            return $this -> returnInfo('排序不符合要求！','',0);
        }
        if(empty($data['id'])){
            $menu = M($this -> table) -> field('id') -> where(['title' => $data['title'],'link' => $data['link']]) ->find();
            if(!empty($menu)){
                return $this -> returnInfo('菜单已存在！','',0);
            }
        }
        return true;
    }

    /**设置返回信息
     * @param string $msg
     * @param $data
     * @param int $status
     * @return array
     */
    public function returnInfo($msg = "", $data, $status = 1){
        return ['msg' => $msg, 'data' => $data, 'status' => $status];
    }

    public function menuList($id,$filed = 'id',$order = 'sort DESC'){
        $menu = M($this -> table) -> field($filed) -> order($order) -> select();
//        if(empty($id)){
//
//        }
        return getTree($menu);
    }

    /**修改状态
     * @param $id
     * @return array
     */
    public function changeState($id){
        if(empty($id)){
            return $this->returnInfo('修改异常！','',0);
        }
        $menu = M($this -> table);
        $menuState = $menu-> field('status') -> where(['id' => $id]) -> find();
        if($menuState['status'] == 0){
            $menu -> where(['id' => $id]) -> setField(['status' => 1]);
            $result = $this -> returnInfo('已开启',1);
        }else{
            $menu -> where(['id' => $id]) -> setField(['status' => 0]);
            $result = $this -> returnInfo('已禁用',0);
        }
        return $result;
    }

    /**菜单删除
     * @param $id
     * @return array
     */
    public function menuDel($id){
        if(empty($id) || $id == 1){
            return $this -> returnInfo('删除异常！','非法操作！','error');
        }
        $menu = M($this -> table);
        $result = $menu -> where(['id' => $id]) -> delete();
        if($result){
            return $this->returnInfo('您已经永久删除了这条信息！','删除成功!','success');
        }else{
            return $this->returnInfo('非法操作！','删除失败！','error');
        }
    }

    public function getInfo($id){
        if(empty($id)){
            return $this->returnInfo('操作异常！','',0);
        }
        $menu = M($this -> table);
        $menuInfo = $menu -> field('id,pid,title,ico,link,sort,status') -> where(['id'=>$id]) -> find();
        if($menuInfo){
            return $this -> returnInfo('',$menuInfo);
        }else{
            return $this -> returnInfo('操作失败！','',0);
        }
    }
    public function getNav(){
        $data = M($this -> table) -> field('id,pid,title,link,ico') -> select();
        $auth = new \Think\auth();
        if($_SESSION['user']['id'] == 1){
            return make_tree($data);
        }
        foreach ($data as $key => $val ) {
            if(!$auth->check($val['link'],session('user.id'))){
                unset($data[$key]);
            }
        }
        return make_tree($data);
    }
}